import 'package:bili_caricature/pages/bloc/cartoon/bloc.dart';
import 'package:bili_caricature/pages/book_shelf/bili_book_shelf/bloc.dart';
import 'package:bili_caricature/pages/cubit/information/cubit.dart';
import 'package:bili_caricature/pages/login/login_bloc.dart';
import 'package:bili_caricature/pages/login/login_page.dart';
import 'package:bili_caricature/pages/mine/bili_mine/cubit.dart';
import 'package:bili_caricature/pages/tabar/view.dart';
import 'package:bili_caricature/routers/routers.dart';
import 'package:bili_caricature/until/shared_preferences.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  setPreferredOrientations();
  await SharedPreferencesUtil.init();
  runApp(
    MultiBlocProvider(
      providers: [
        BlocProvider<MessageModuleCubit>(
          create: (BuildContext context) => MessageModuleCubit(),
        ),
        BlocProvider<CartoonBloc>(
          create: (BuildContext context) => CartoonBloc(),
        ),
        BlocProvider<BlocStaggeredGridViewBloc>(
          create: (BuildContext context) => BlocStaggeredGridViewBloc(),
        ),
        BlocProvider<BiliMineCubit>(
          create: (BuildContext context) => BiliMineCubit(),
        ),
        BlocProvider<LoginBloc>(
          create: (BuildContext context) => LoginBloc(),
        ),
      ],
      child: MyApp(),
    ),
  );
}

class MyApp extends StatefulWidget {
  MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
  @override
  void initState() {
    super.initState();
    Routes.initRoutes();
  }

  MaterialApp _materialApp(BuildContext context) {
    String phoneNumber = SharedPreferencesUtil.sharedPreferences.getString("phoneNumber") ?? '';

    return MaterialApp(
      home: phoneNumber.isNotEmpty ? const BiliTabarPage() : const LoginPage(),
      builder: (context, widget) {
        return MediaQuery(
          ///设置文字大小不随系统设置改变
          data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
          child: FlutterEasyLoading(child: widget),
        );
      },
      navigatorKey: navigatorKey,
      locale: const Locale('zh', 'CH'),
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate
      ],
      supportedLocales: const [
        Locale('zh', 'CH'),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      builder: (BuildContext context, Widget? child) {
        return _materialApp(context);
      },
    );
  }
}

/// 强制app竖屏
void setPreferredOrientations() async {
  await SystemChrome.setPreferredOrientations(
    [
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ],
  );
}
